package cn.sxt.common.struts2.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSONObject;

import cn.sxt.common.util.DateFormatUtil;
import cn.sxt.common.util.PageInfoUtil;

/**
 * 所有Action的公共代码
 * @author wangshSxt
 *
 */
public class BaseAction
{
	/* 公共的request */
	protected HttpServletRequest request = ServletActionContext.getRequest() ;
	protected HttpServletResponse response = ServletActionContext.getResponse() ;
	
	@Resource
	protected DateFormatUtil dateFormatUtil ; 
	
	/* 提示信息 */
	protected String info ;
	
	/**
	 * 拼装分页对象
	 * @param request
	 * @return
	 */
	protected PageInfoUtil proccedPageInfo(HttpServletRequest request)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/* 接收参数 */
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		/* 赋值 */
		try
		{
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.parseInt(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil ; 
	}
	
	/**
	 * 封装JUI要求的返回结果
	 * 
	 * { "statusCode":"200", "message":"\u64cd\u4f5c\u6210\u529f", "navTabId":"", "rel":"", "callbackType":"", "forwardUrl":"", "confirmMsg":"" }
	 * 
	 * @return
	 */
	protected JSONObject printJUIResult(String info)
	{
		/* 接收参数 */
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String callbackType = request.getParameter("callbackType");
		String forwardUrl = request.getParameter("forwardUrl");
		String confirmMsg = request.getParameter("confirmMsg");
		
		if(navTabId == null)
		{
			navTabId = "" ; 
		}
		if(rel == null)
		{
			rel = "" ; 
		}
		if(callbackType == null)
		{
			callbackType = "" ; 
		}
		if(forwardUrl == null)
		{
			forwardUrl = "" ; 
		}
		if(confirmMsg == null)
		{
			confirmMsg = "" ; 
		}
		
		/* { "statusCode":"200", "message":"\u64cd\u4f5c\u6210\u529f", "navTabId":"", "rel":"", "callbackType":"", "forwardUrl":"", "confirmMsg":"" }  */
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", info);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON ; 
	}
	
	/**
	 * 返回结果给客户端
	 * @param result
	 * @throws IOException 
	 */
	protected void printOut(String result) throws IOException
	{
		PrintWriter out = response.getWriter() ; 
		out.println(result);
		out.flush();
		out.close();
	}

	public String getInfo()
	{
		return info;
	}

	public void setInfo(String info)
	{
		this.info = info;
	}

	public HttpServletRequest getRequest()
	{
		return request;
	}

	public HttpServletResponse getResponse()
	{
		return response;
	}
}
